home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12439 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need code to remove non-adjacent duplicate lines
  5. Date: Sat, 30 Mar 96 20:49:01 GMT
  6. Organization: none
  7. Message-ID: <828218941snz@genesis.demon.co.uk>
  8. References: <1996Mar27.113154.14694@schbbs.mot.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <1996Mar27.113154.14694@schbbs.mot.com> ghelm "george_helm" writes:
  15.  
  16. >Does anyone have C code that will remove *non-adjacent* duplicate
  17. >lines from an ascii file ? I need to retain the original file
  18. >format so I can't use simple stuff like sort -u or uniq in UNIX.
  19. >Help on this is greatly appreciated.
  20.  
  21. How you approach this depends on whether the file is small enough to be
  22. stored in memory. If it is then you build a lookup datastructure keyed
  23. on the contents of the line. If a new line matches an entry in the
  24. datastructure you ignore it, otherwise add it and output it. The following
  25. demonstrates the algorithm:
  26.  
  27. awk '
  28. {
  29.     if (!a[$0]) {
  30.         a[$0] = 1
  31.         print
  32.     }
  33. }'
  34.  
  35. I leave it as an exercise for the reader to translate this to C! :-)
  36.  
  37. -- 
  38. -----------------------------------------
  39. Lawrence Kirby | fred@genesis.demon.co.uk
  40. Wilts, England | 70734.126@compuserve.com
  41. -----------------------------------------
  42.